package leetcode_1701_1800;

import java.util.Arrays;

public class LeeCode_1706 {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(findBall(new int[][]{{1, 1, 1, -1, -1}, {1, 1, 1, -1, -1}, {-1, -1, -1, 1, 1}, {1, 1, 1, 1, -1}, {-1, -1, -1, -1, -1}})));
    }
    private static int[] findBall(int[][] grid) {
        int n = grid[0].length;
        int[] ans = new int[n];
        for (int i = 0; i < n; i++) {
            ans[i] = dfs(grid, 0, i);
        }
        return ans;
    }
    private static int dfs(int[][] grid, int x, int y){
        int m = grid.length, n = grid[0].length;
        if (x == m)
            return y;
        if (grid[x][y] == 1){
            if (y + 1 == n)
                return -1;
            if (grid[x][y + 1] == 1){
                return dfs(grid, x + 1, y + 1);
            }else {
                return -1;
            }
        }else {
            if (y - 1 == -1)
                return -1;
            if (grid[x][y - 1] == -1){
                return dfs(grid, x + 1, y - 1);
            }else {
                return -1;
            }
        }
    }
}
